
typedef int STDataType;
typedef struct stack
{
	int* a;
	int top;
	int capacity;
}ST;

void STInit(ST* ps);
void STPush(ST* ps,STDataType x);
void STPop(ST* ps);
STDataType STTop(ST* ps);
bool STEmpty(ST* ps);
int STSize(ST* ps);
void STDestroy(ST* ps);
void STInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;  //top初始化位0,top的值可以表示栈元素的个数
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* ret = (STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		ps->a = ret;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top);

	ps->top--;
}

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(ps->top);

	return ps->a[ps->top - 1];
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a == NULL;
	ps->top = ps->capacity = 0;
}

typedef struct {
    ST Queuepush;
    ST Queuepop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue *obj=(MyQueue*)malloc(sizeof(MyQueue));
    STInit(&obj->Queuepush);
    STInit(&obj->Queuepop);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    STPush(&obj->Queuepush,x);
}

int myQueuePeek(MyQueue* obj) {
    if(STEmpty(&obj->Queuepop))
    {
        while(!STEmpty(&obj->Queuepush))
        {
            STPush(&obj->Queuepop,STTop(&obj->Queuepush));
            STPop(&obj->Queuepush);
        }
    }
    return STTop(&obj->Queuepop);
}

int myQueuePop(MyQueue* obj) {
    int front = myQueuePeek(obj);
    STPop(&obj->Queuepop);
    return front;
}

bool myQueueEmpty(MyQueue* obj) {
    return STEmpty(&obj->Queuepop)&&STEmpty(&obj->Queuepush);
}

void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->Queuepop);
    STDestroy(&obj->Queuepush);
    free(obj);
}